package com.sinodata.bsm.cicp.collector.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.CommandReader;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-1-31 下午2:42:11          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class ServerTimeDefferenceCollector extends Collector {

    private static final Logger logger = Logger.getLogger(ServerTimeDefferenceCollector.class);
    private static final String shell = "date +%Y-%m-%d\\ %H:%M:%S";
    private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public String[] getPropMeta() {
        //return new String[]{"第一台服务器时间","第二台服务器时间","时间差"};
        return new String[] { "$1", "$2", "$3" };
    }

    @Override
    public String[] getParamMeta() {
        //return new String []{"ip1","ip2"}
        return new String[] { "$1", "$2" };
    }

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        String[] props = this.getPropMeta();
        String ip1 = this.getParamMap().get("$1");
        String ip2 = this.getParamMap().get("$2");
        List<ResPropertyValue> list = new ArrayList<ResPropertyValue>();
        ResPropertyValue resPropertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[props.length];
        BufferedReader reader = null;
        String strLine = null;
        String firstTime = null;
        String secondTime = null;
        String differenceTime = null;
        try {
            reader = CommandReader.getReader(ip1, shell, 0, 0);
            while ((strLine = reader.readLine()) != null) {
                firstTime = strLine.trim();
            }
            reader = CommandReader.getReader(ip2, shell, 0, 0);
            while ((strLine = reader.readLine()) != null) {
                secondTime = strLine.trim();
            }
            differenceTime = Math.abs(format.parse(firstTime).getTime() - format.parse(secondTime).getTime()) / 1000 + "";
            propValues[0] = ResPropertyValue.createPropValue(firstTime, props[0]);
            propValues[1] = ResPropertyValue.createPropValue(secondTime, props[1]);
            propValues[2] = ResPropertyValue.createPropValue(differenceTime, props[2]);
            resPropertyValueX.setValues(propValues);
            list.add(resPropertyValueX);
        } catch (IOException e1) {
            logger.error("failed to collect ServerTimeDefferenceCollector");
            throw new CollectorException(e1);
        } catch (ParseException e2) {
            logger.error("failed to parse time in ServerTimeDefferenceCollector");
            throw new CollectorException(e2);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e3) {
                    logger.error("failed to close reader when collect ServerTimeDefferenceCollector");
                    throw new CollectorException(e3);
                }
            }
        }
        return list;
    }

}
